package starcoast.bocai.com.starcoast.activitys.work;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.adapter.PostPhotoAdapter;
import starcoast.bocai.com.starcoast.common.BaseActivity;
import starcoast.bocai.com.starcoast.pressent.PhotoPressent;
import starcoast.bocai.com.starcoast.view.MyGridView;

/**
 * Created by admin on 2016/6/13.
 */
public class LogisticsActivity extends BaseActivity implements View.OnClickListener, PhotoPressent {
    @Bind(R.id.logistics_photo)
    MyGridView logistics_photo;
    PostPhotoAdapter adapter;
    ArrayList<String> photoUrls = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        ButterKnife.bind(this);
        initToolbar("物业服务");
        initToolBack();
        initview();
    }
    public void initview(){
        adapter = new PostPhotoAdapter(this,photoUrls,this);
        logistics_photo.setAdapter(adapter);
    }
    private void startChosePhoto(int what) {
        PhotoPickerIntent intent = new PhotoPickerIntent(this);
        intent.setPhotoCount(3-photoUrls.size());
        intent.setShowCamera(true);
        startActivityForResult(intent, what);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 1000) {
            if (data!=null) {
                ArrayList<String> photoString = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                if (data != null) {
                    for (int i = 0; i < photoString.size(); i++) {
                        Log.e("photoString", photoString.get(i));
                        if (photoUrls.size()<3) {
                            photoUrls.add(photoString.get(i));
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void startCamera() {
        checkCameraPermission();
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void checkCameraPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            //应用还照相 的权限，询问是否允许
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 111);
        } else {
            checkReadPermission();
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void checkReadPermission() {
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //应用还未获取读取本地文件 的权限，询问是否允许
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 222);
        } else {
            startChosePhoto(1000);
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 111) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                checkReadPermission();
            } else {
                showToast("权限被禁止，无法打开相机");
            }
        }else if(requestCode == 222){
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startChosePhoto(1000);
            } else {
                showToast("权限被禁止，无法打开相册");
            }
        }
    }

    @Override
    public void onClick(View v) {

    }
}
